const app = getApp()
const util = require('../../../utils/util.js')

Page({

  data: {
    list: [],
    options: null
  },

  onLoad: function(options) {
    this.setData({
      options: options
    })
    if (options.type == 'collect') {
      this.getCollectList()
      wx.setNavigationBarTitle({
        title: '我的收藏',
      })
    } else if (options.type == 'history') {
      this.getHistoryList()
      wx.setNavigationBarTitle({
        title: '历史记录',
      })
    }
  },

  onShow: function() {
    if (this.data.options) {
      this.onLoad(this.data.options)
    }
  },

  onShareAppMessage: function() {
    return {
      title: '我的收藏',
    }
  },

  // 获取收藏列表
  getCollectList: function() {
    var that = this
    wx.request({
      url: app.globalData.url + 'accs/get_favor_list',
      method: 'POST',
      data: {
        user_id: app.globalData.userId,
      },
      success: function(res) {
        var list = res.data.data
        if (list) {
          that.setData({
            list: list
          })
        }
      }
    })
  },

  // 获取播放历史列表
  getHistoryList: function() {
    var historyList = wx.getStorageSync('historyList')
    if (historyList) {
      historyList = util.beSet(historyList)
      wx.setStorageSync('historyList', historyList)
      this.setData({
        list: historyList
      })
    }
  },

  gotoDetail: function (e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id
    })
  }

})